package com.qianyu.project.core.mapper.provider;

import com.qianyu.project.core.bean.FlowNode;
import org.apache.ibatis.jdbc.SQL;

import java.util.List;

/**
 * @author gs
 * @date create in 2020/12/11 16:07
 */
public class FlowNodeProvider {

    public String selectFirtstNode(Integer flowId){
        return new SQL(){
            {
                SELECT("*");
                FROM("flow_node");
                WHERE("flowId=#{value}");
                WHERE("seq=1");
                WHERE("isvalid=1");
            }
        }.toString();
    }

    public String selectNextNode(FlowNode flowNode){
        return new SQL(){
            {
                SELECT("*");
                FROM("flow_node");
                WHERE("flowId=#{flowId}");
                WHERE("seq=#{seq}+1");
                WHERE("isvalid=1");
            }
        }.toString();
    }

    public String saveBatch(List<FlowNode> nodeList){
        return new SQL(){
            {
                INSERT_INTO("flow_node");
                nodeList.iterator().forEachRemaining(i->{
                    String sign = i.getSign()==null?"null":"'"+i.getSign()+"'";
                    String field = i.getField()==null?"null":"'"+i.getField()+"'";
                    String count = i.getCount()==null?"null":i.getCount().toString();
                    INTO_VALUES("null", "'"+i.getFlowNodeName()+"'",i.getFlowNodeRole().toString(),i.getSeq().toString(),"'"+i.getFlowNodeRemark()+"'",i.getFlowId().toString(),sign,field,count,i.getIsvalid().toString());
                    ADD_ROW();
                });
            }
        }.toString();
    }
}
